package com.base.service;

import java.sql.Timestamp;
import java.util.*;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import tool.ClientTool;
import tool.JsonTool;
import tool.TimeTool;

import com.base.bean.*;
import com.base.dao.*;
import com.base.domain.*;

/**
 * 待完成功能： getDetailByCid() getList(int con,int page,int isDesc)
 * con可以是：1/2/3/4/5的形式 单选按钮的值
 * 
 * @author Administrator
 * 
 */

public class ClientDetailService {

	// 单例模式 start
	private volatile static ClientDetailService instance = null;
	private ClientService cs;

	// 条件查询需要用到的枚举类型
	static enum CHECK_TYPE {
		DEFAULT, MODIFIED, CREATED, CLIENT_TYPE, SELL_PLAN, CITY, PROVINCE
	}

	private ClientDetailService() {
		this.cs=ClientService.getInstance();
	}
	
	

	public static ClientDetailService getInstance() {
		if (instance == null) {
			synchronized (ClientDetailService.class) {// 1
				if (instance == null) {// 2
					instance = new ClientDetailService();// 3
				}
			}
		}
		return instance;
	}

	//get clientDetail by cid
	public ClientDetail getClientDetail(Integer cid,Integer owner){
		ClientExtend ce=null;
		try{
			ClientDetail cd=new ClientDetail();	
			ClientBase cb=cs.getClientBaseDao().findByHql("from ClientBase cb where  cb.cid="+cid+" and cb.owner="+owner ).get(0);
			try{
			ce=cs.getClientExtendDao().findByHql("from ClientExtend ce where ce.cid="+cid+" and ce.owner="+owner).get(0);
			}catch(Exception e){
				ce=new ClientExtend();
				ce.setCid(cid);
				ce.setOwner(owner);
				
			}
			List<ClientPhone> cps=cs.getClientPhoneDao().findByHql("from ClientPhone cp where  cp.state=1 and cp.cid="+cid);
			
			cd.setCid(cid);
			cd.setClientBase(cb);
			cd.setClientExtend(ce);
			cd.setClientPhones(cps);
		
		return cd;
		}catch(Exception e){
			return null;
		}
	}
	
	public int getTotal(Integer owner,Integer state){
		Session session = null;
		int count=0;
	try{	
		session = ClientService.getInstance().getClientBaseDao()
				.getSessionFactory().getCurrentSession();
		Criteria criteria = null;
		Transaction tx = null;

		// try begin
		tx = session.beginTransaction();
		criteria = session.createCriteria(ClientBase.class);
		criteria.add(Restrictions.eq("owner", owner)); // 属于自己的
		criteria.add(Restrictions.eq("state", state)); // state=1 有效状态
		criteria.setProjection(Projections.rowCount());
		count=Integer.parseInt(criteria.uniqueResult().toString());
		tx.commit();
		return count;
	}catch(Exception e){
		e.printStackTrace();
		return 0;
	}finally{
		if (session.isOpen())
			session.close();
	}
	}
	
	public List<Integer> getCidsByPage(Integer offSet,Integer pageSize,Integer owner,Integer state,boolean isModifyDesc){
		Session session = null;
		List<Integer> cids=null;
	try{
		session = ClientService.getInstance().getClientBaseDao()
				.getSessionFactory().getCurrentSession();
		Criteria criteria = null;
		Transaction tx = null;

		// try begin
		tx = session.beginTransaction();
		criteria = session.createCriteria(ClientBase.class);
		criteria.add(Restrictions.eq("owner", owner)); // 属于自己的
		criteria.add(Restrictions.eq("state", state)); // state=1 有效状态
		if(isModifyDesc)							   // 修改时间排序
			criteria.addOrder(Order.desc("modified"));
		else
			criteria.addOrder(Order.asc("modified"));
		
		criteria.setFirstResult(offSet);  	//分页
		criteria.setMaxResults(pageSize);
		
		ProjectionList proList = Projections.projectionList();// 设置投影集合
		proList.add(Projections.groupProperty("cid"));
		criteria.setProjection(proList);
		cids = criteria.list();
		tx.commit();
		return cids;
	}catch(Exception e){
		e.printStackTrace();
		return null;
	}finally{
		if (session.isOpen())
			session.close();
	}
	}
	
	
	
	
	
	
	
	// 测试
	public static void main(String[] args){
		int count=ClientDetailService.getInstance().getTotal(10, 1);
//		System.out.println(count);
		
		List<Integer> list=ClientDetailService.getInstance().getCidsByPage(0, 10, 10, 1, true);
		for(Integer i:list){
		try{
//			System.out.println(i);
			ClientDetail cd=ClientDetailService.getInstance().getClientDetail(i,10);
//			System.out.println(cd.getClientBase().getCreated()+"|"+cd.getCid()+"|"+cd.getClientBase().getCreator()+"|"+cd.getClientPhones().get(0).getPhone());
		}catch(Exception e){
			e.printStackTrace();
//			continue;
		}
		
		}
		System.out.println("end");
	}
	
	

}
